package com.awesome.common.widget

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.widget.FrameLayout
import com.awesome.common.R

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2021/5/8 17:43
 * Description:圆角布局
 * 用该布局包裹时,原来的根布局的 margin 需移到该控件中
 * 不能在该控件中设置 padding 及背景色
 * eg
 * <LinearLayout
 * android:layout_width="match_parent"
 * android:layout_height="match_parent"
 * android:layout_marginStart="@dimen/px32"
 * android:layout_marginEnd="@dimen/px32"
 * android:background="#FFFFFFFF"
 * android:gravity="center_vertical"
 * android:orientation="horizontal"
 * android:paddingStart="@dimen/px16"
 * android:paddingEnd="@dimen/px16">
 * <p>
 * <ImageView
 * android:layout_width="wrap_content"
 * android:layout_height="wrap_content"
 * android:src="@drawable/conversion_iv_search_src" />
 * <p>
 * <TextView
 * android:layout_width="wrap_content"
 * android:layout_height="wrap_content"
 * android:layout_marginStart="@dimen/px16"
 * android:text="@string/add_friend_et_keyword_hint"
 * android:textColor="#FF999999"
 * android:textSize="@dimen/px26" />
 * </LinearLayout>
 * <p>
 * 使用该控件包裹时应变成这样
 * <com.jeejio.pub.view.widget.RoundCornerLayout
 * android:layout_width="match_parent"
 * app:cornerRadius="@dimen/px12"
 * android:layout_marginStart="@dimen/px32"
 * android:layout_marginEnd="@dimen/px32"
 * android:layout_height="match_parent">
 * <p>
 * <LinearLayout
 * android:layout_width="match_parent"
 * android:layout_height="match_parent"
 * android:background="#FFFFFFFF"
 * android:gravity="center_vertical"
 * android:orientation="horizontal"
 * android:paddingStart="@dimen/px16"
 * android:paddingEnd="@dimen/px16">
 * <p>
 * <ImageView
 * android:layout_width="wrap_content"
 * android:layout_height="wrap_content"
 * android:src="@drawable/conversion_iv_search_src" />
 * <TextView
 * android:layout_width="wrap_content"
 * android:textSize="@dimen/px26"
 * android:textColor="#FF999999"
 * android:layout_marginStart="@dimen/px16"
 * android:text="@string/add_friend_et_keyword_hint"
 * android:layout_height="wrap_content"/>
 * </LinearLayout>
 * </com.jeejio.pub.view.widget.RoundCornerLayout>
 */
class RoundCornerLayout : FrameLayout {
    companion object {
        private const val ALL = 0x1111
        private const val LEFT_TOP = 0x0001
        private const val RIGHT_TOP = 0x0010
        private const val LEFT_BOTTOM = 0x0100
        private const val RIGHT_BOTTOM = 0x1000
    }

    private val paint = Paint()
    private val roundCornerRectF = RectF()
    private val path = Path()
    private val cornerRadii = FloatArray(8)

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        paint.isAntiAlias = true
        // 设置画笔绘制两部分的交集
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerLayout)
        try {
            val cornerRadius = typedArray.getDimensionPixelSize(R.styleable.RoundCornerLayout_radius, 15)
            val corner = typedArray.getInteger(R.styleable.RoundCornerLayout_position, ALL)
            if (corner and LEFT_TOP == LEFT_TOP) {
                cornerRadii[0] = cornerRadius.toFloat()
                cornerRadii[1] = cornerRadius.toFloat()
            }
            if (corner and RIGHT_TOP == RIGHT_TOP) {
                cornerRadii[2] = cornerRadius.toFloat()
                cornerRadii[3] = cornerRadius.toFloat()
            }
            if (corner and LEFT_BOTTOM == LEFT_BOTTOM) {
                cornerRadii[6] = cornerRadius.toFloat()
                cornerRadii[7] = cornerRadius.toFloat()
            }
            if (corner and RIGHT_BOTTOM == RIGHT_BOTTOM) {
                cornerRadii[4] = cornerRadius.toFloat()
                cornerRadii[5] = cornerRadius.toFloat()
            }
        } finally {
            typedArray.recycle()
        }
    }

    override fun dispatchDraw(canvas: Canvas) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // 保存画布当前状态
            canvas.save()
            path.reset()
            path.addRoundRect(roundCornerRectF, cornerRadii, Path.Direction.CW)
            // 裁剪圆角
            canvas.clipPath(path)
            // 绘制子控件
            super.dispatchDraw(canvas)
            // 恢复画布状态
            canvas.restore()
        } else {
            // 保存画布当前状态
            canvas.saveLayer(roundCornerRectF, null, Canvas.ALL_SAVE_FLAG)
            // 绘制子控件
            super.dispatchDraw(canvas)
            // 绘制圆角
            path.reset()
            path.addRoundRect(roundCornerRectF, cornerRadii, Path.Direction.CW)
            canvas.drawPath(path, paint)
            // 恢复画布状态
            canvas.restore()
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        roundCornerRectF.set(0f, 0f, w.toFloat(), h.toFloat())
    }
}